#include "TileMenu.h"

template <class T>
TileMenu<T>::TileMenu(Gosu::Graphics& gfx, int posX,int posY,int columns,int rows, std::vector<T> *tiles, int size) : graphics(&gfx){

	this->posX = posX;
	this->posY = posY;
	this->columns = columns;
	this->rows = rows;
	this->size = size;

	this->tiles = *tiles;
	this->clickedTile = this->tiles.at(0);

	std::vector<T>::iterator it = this->tiles.begin();
	std::wstring filename;

	while(it != this->tiles.end()){

		filename = Gosu::sharedResourcePrefix() + Gosu::widen((*it).path);

		if(size == 22){
			this->images.push_back(new Gosu::Image(*graphics, filename,10,10,20,20,true));
		}else{
			this->images.push_back(new Gosu::Image(*graphics, filename, true));
		}

		it++;

	}	


}

template <class T>
void TileMenu<T>::draw(){




	std::vector<Gosu::Image *>::iterator it = images.begin();

	int i = 0;
	std::wstring filename;

	while(it != images.end()){

		(*it)->draw(posX + 5 + size * (i % this->columns), posY +5 + size * (i / this->columns), 1);
		
		i++;
		it++;

	}

}
template <class T>
T TileMenu<T>::getTile(double x, double y){


	int col = (int)((x - (double)posX) / (double)size);
	int row = (int)((y - (double)posY) / (double)size);

	if((unsigned int)(col + row * this->columns) < tiles.size())
		clickedTile = tiles.at(col + row * this->columns);


	return clickedTile;

}

template <class T>
bool TileMenu<T>::onMenu(double x, double y){

	
	return ( x > posX && x < posX + size * this->columns && y > posY && y < posY + size + size * (tiles.size()-1) / this->columns );
}